package com.fireline.beauty.member;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ControlEditor;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;

import com.fireline.beauty.cashier.HyxxTextPopup;
import com.zgw.fireline.base.controls.DataCombo;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.frame.IFunction;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import com.zgw.fireline.base.widgets.Batch;
import com.zgw.fireline.base.IDataBaseProvide;
import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.widgets.BatchItem;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

/**
 * 会员预约
 * */
public class HyyuEdit implements IFunction {

	private Shell shell;
	private Text text;
	private Text bzText;

	private CLabel lblNewLabel_1;
	private CLabel label_5;
	private CLabel label_4;
	private String hybh;
	/**
	 * @wbp.nonvisual location=23,472
	 */
	private final Batch batch = new Batch(SqliteJdbcImpl.INSTANCE);
	private DateTime rqDate;
	private DateTime jssjTime;
	private DateTime kssjTime;
	private List xmlist;
	/**
	 * @wbp.nonvisual location=97,502
	 */
	private final Dataset ygSet = new Dataset(true, "ygxx",
			SqliteJdbcImpl.INSTANCE);
	/**
	 * @wbp.nonvisual location=217,502
	 */
	private final Dataset cwSet = new Dataset("select * from cwsz",
			SqliteJdbcImpl.INSTANCE);
	private Composite content;
	public int result;

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	public Control createControl(Composite parent) {
		this.shell = parent.getShell();
		ygSet.setKeyColumns(new String[] {});
		cwSet.setKeyColumns(new String[] {});

		Composite content = new Composite(parent, SWT.NONE);
		GridLayout gl_content = new GridLayout(1, false);
		gl_content.verticalSpacing = 10;
		content.setLayout(gl_content);

		Group group = new Group(content, SWT.NONE);
		GridLayout gl_group = new GridLayout(5, false);
		gl_group.marginWidth = 10;
		gl_group.marginHeight = 10;
		gl_group.verticalSpacing = 10;
		gl_group.horizontalSpacing = 0;
		group.setLayout(gl_group);
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1,
				1));
		group.setText("会员信息");

		Label label = new Label(group, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label.setText("查找会员：");

		text = new Text(group, SWT.BORDER);
		GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, false, false, 1,
				1);
		gd_text.widthHint = 100;
		text.setLayoutData(gd_text);
		final HyxxTextPopup popup = new HyxxTextPopup(text, false,
				new Listener() {
					@Override
					public void handleEvent(Event event) {
						setValues((Dataset) event.data);
						text.getParent().layout();
					}
				});
		Label lblNewLabel = new Label(group, SWT.NONE);
		lblNewLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				popup.show();
			}
		});
		GridData gd_lblNewLabel = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_lblNewLabel.horizontalIndent = 2;
		lblNewLabel.setLayoutData(gd_lblNewLabel);
		lblNewLabel.setToolTipText("点击查找");
		lblNewLabel.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		lblNewLabel.setImage(SWTResourceManager.getImage(HyyuEdit.class,
				"/com/fireline/beauty/image/query.png"));

		Label label_1 = new Label(group, SWT.NONE);
		GridData gd_label_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_label_1.horizontalIndent = 25;
		label_1.setLayoutData(gd_label_1);
		label_1.setText("会员姓名：");

		lblNewLabel_1 = new CLabel(group, SWT.NONE);
		lblNewLabel_1
				.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		lblNewLabel_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		Label label_2 = new Label(group, SWT.NONE);
		label_2.setText("会员卡名称：");

		label_5 = new CLabel(group, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));
		label_5.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		new Label(group, SWT.NONE);

		Label label_3 = new Label(group, SWT.NONE);
		GridData gd_label_3 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_label_3.horizontalIndent = 25;
		label_3.setLayoutData(gd_label_3);
		label_3.setText("联系电话：");

		label_4 = new CLabel(group, SWT.NONE);
		label_4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));
		label_4.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));

		Group group_1 = new Group(content, SWT.NONE);
		GridLayout gl_group_1 = new GridLayout(5, false);
		gl_group_1.marginHeight = 10;
		gl_group_1.marginWidth = 10;
		gl_group_1.verticalSpacing = 10;
		gl_group_1.horizontalSpacing = 0;
		group_1.setLayout(gl_group_1);
		group_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		group_1.setText("预约项目及时间");

		Label label_6 = new Label(group_1, SWT.NONE);
		label_6.setText("预约日期：");

		rqDate = new DateTime(group_1, SWT.BORDER | SWT.DROP_DOWN);

		Label label_7 = new Label(group_1, SWT.NONE);
		GridData gd_label_7 = new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1);
		gd_label_7.horizontalIndent = 20;
		label_7.setLayoutData(gd_label_7);
		label_7.setText("预约项目：");

		xmlist = new List(group_1, SWT.BORDER);
		xmlist.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 3));

		ToolBar toolBar = new ToolBar(group_1, SWT.FLAT | SWT.RIGHT
				| SWT.VERTICAL);
		GridData gd_toolBar = new GridData(SWT.LEFT, SWT.TOP, false, false, 1,
				3);
		gd_toolBar.horizontalIndent = 5;
		toolBar.setLayoutData(gd_toolBar);

		ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FwxmSearchDialog dlg = new FwxmSearchDialog(shell, SWT.NONE);
				int[] indexs = (int[]) dlg.open();
				if (indexs != null) {
					for (int i = 0; i < indexs.length; i++) {
						dlg.fwxmSet.setSelected(indexs[i]);
						xmlist.add(dlg.fwxmSet.getString("xmmc"));
						xmlist.setSelection(xmlist.getItemCount() - 1);
					}
				}
			}
		});
		tltmNewItem.setText("添加");
		tltmNewItem.setImage(null);

		ToolItem tltmNewItem_1 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (xmlist.getSelectionCount() > 0) {
					xmlist.remove(xmlist.getSelectionIndex());
				}
			}
		});
		tltmNewItem_1.setText("删除");

		Label label_8 = new Label(group_1, SWT.NONE);
		label_8.setText("开始时间：");

		kssjTime = new DateTime(group_1, SWT.BORDER | SWT.TIME);
		kssjTime.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));
		new Label(group_1, SWT.NONE);

		Label label_9 = new Label(group_1, SWT.NONE);
		label_9.setText("结束时间：");

		jssjTime = new DateTime(group_1, SWT.BORDER | SWT.TIME);
		jssjTime.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));
		new Label(group_1, SWT.NONE);

		Group group_2 = new Group(content, SWT.NONE);
		GridLayout gl_group_2 = new GridLayout(4, false);
		gl_group_2.marginWidth = 10;
		gl_group_2.marginHeight = 10;
		gl_group_2.verticalSpacing = 10;
		gl_group_2.horizontalSpacing = 0;
		group_2.setLayout(gl_group_2);
		group_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		group_2.setText("预约技术及床位");

		Label lblNewLabel_2 = new Label(group_2, SWT.NONE);
		lblNewLabel_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel_2.setText("预约技师：");

		DataCombo dataCombo = new DataCombo(group_2, SWT.READ_ONLY, ygSet);
		dataCombo.setShowText("${ygmc}");
		dataCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label label_10 = new Label(group_2, SWT.NONE);
		GridData gd_label_10 = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		gd_label_10.horizontalIndent = 20;
		label_10.setLayoutData(gd_label_10);
		label_10.setText("预约床位：");

		DataCombo cwCombo = new DataCombo(group_2, SWT.READ_ONLY, cwSet);
		cwCombo.setShowText("${ch}");
		cwCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label label_11 = new Label(group_2, SWT.NONE);
		label_11.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		label_11.setText("备注信息：");

		bzText = new Text(group_2, SWT.BORDER);
		bzText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3,
				1));

		Composite composite = new Composite(content, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		composite.setLayout(new GridLayout(2, false));

		Button btnf = new Button(composite, SWT.NONE);
		btnf.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSave();
			}
		});
		GridData gd_btnf = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1,
				1);
		gd_btnf.heightHint = 25;
		gd_btnf.widthHint = 80;
		btnf.setLayoutData(gd_btnf);
		btnf.setText("确定（F5）");

		Button btnesc = new Button(composite, SWT.NONE);
		GridData gd_btnesc = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_btnesc.heightHint = 25;
		gd_btnesc.widthHint = 80;
		btnesc.setLayoutData(gd_btnesc);
		btnesc.setText("取消（ESC）");
		setParams();
		this.content = content;
		createGUIAfter();
		return content;
	}

	protected void doSave() {
		if (this.hybh == null) {
			MessageDialog.openInformation(shell, "提示", "请选择一个会员");
			text.setFocus();
			return;
		}
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		cal.set(rqDate.getYear(), rqDate.getMonth(), rqDate.getDay(),
				kssjTime.getHours(), kssjTime.getMinutes());
		Date kssj = cal.getTime();
		cal.set(rqDate.getYear(), rqDate.getMonth(), rqDate.getDay(),
				jssjTime.getHours(), jssjTime.getMinutes());
		Date jssj = cal.getTime();
		StringBuffer xmmc = new StringBuffer();
		for (String s : xmlist.getItems()) {
			if (xmlist.indexOf(s) != 0) {
				xmmc.append(",");
			}
			xmmc.append(s);
		}
		BatchItem item = new BatchItem(batch);
		item.setCommand("insert into hyyu (hybh,kssj,jssj,fwsj,xmmc,ygbh,ch,bz,zt) "
				+ "values(?,?,?,?,?,?,?,?,?)");
		item.setName("保存预约记录");
		item.setParam(0, hybh);
		item.setParam(1, format.format(kssj));
		item.setParam(2, format.format(jssj));
		item.setParam(3, (jssj.getTime() - kssj.getTime()) / 1000 / 60);// 服务时间（分钟）
		item.setParam(4, xmmc);
		item.setParam(5, ygSet.getString("ygbh"));
		item.setParam(6, cwSet.getString("ch"));
		item.setParam(7, bzText.getText());
		item.setParam(8, 1);
		try {
			batch.execute();
			MessageDialog.openInformation(shell, "提示", "预约成功!");
			result = SWT.OK;
			this.content.dispose();
		} catch (Exception e) {
			e.printStackTrace();
			MessageDialog.openError(shell, "异常", "预约失败:" + e.getMessage());
		}
	}

	protected void setParams() {

	}

	protected void setValues(Dataset dataset) {
		lblNewLabel_1.setText(dataset.getValue("hyxm", String.class, ""));
		label_5.setText(dataset.getValue("hykmc", String.class, ""));
		label_4.setText(dataset.getValue("lxdh", String.class, ""));
		this.hybh = dataset.getString("hybh");
		text.setText(hybh);
	}

	protected void createGUIAfter() {
		ygSet.update();
		cwSet.update();
	}
}
